package com.course.controller;


import com.course.entity.ResultBody;
import com.course.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/message")
@CrossOrigin(origins = "*")
public class MessageController {

    @Autowired
    private MessageService messageService;
//
//    //    学生查看自己所选课程
//    @RequestMapping("/getusercourse")
//    public ResultBody getusercourse(@RequestBody Map<String, Object> map) {
//        return messageService.getusercourse(map);
//    }

    //    老师查看自己课程学生
    @RequestMapping("/getteachercourse")
    public ResultBody getteachercourse(@RequestBody Map<String, Object> map) {
        return messageService.getteachercourse(map);
    }

    //学生给老师发送信息
    @RequestMapping("/sendmessage")
    public ResultBody sendmessage(@RequestBody Map<String, Object> map) {
        if (map.get("messagenumber") == null || map.get("messagecourse") == null || map.get("messagetext") == null||map.get("messagetype")==null) {
            return new ResultBody(404, "参数错误", false);
        }
        String messagetype = map.get("messagetype").toString();
        Object object1 = map.get("messagenumber");
        Object object2 = map.get("messagecourse");
        String messagetext = map.get("messagetext").toString();
        Integer messagecourse;
        Integer messagenumber;
        if (object2 instanceof String) {
            messagecourse = Integer.parseInt((String) object2);
        } else if (object2 instanceof Integer) {
            messagecourse = (Integer) object2;
        } else
            return new ResultBody(400, "未知参数类型", false);

        if (object1 instanceof String) {
            messagenumber = Integer.parseInt((String) object1);
        } else if (object1 instanceof Integer) {
            messagenumber = (Integer) object1;
        } else
            return new ResultBody(400, "未知参数类型", false);
        return messageService.sendmessage(messagenumber, messagecourse, messagetext,messagetype);
    }

    //展示发送的信息
    @RequestMapping("/displaysmessage")
    public ResultBody displaysmessage(@RequestBody Map<String, Object> map) {
        if (map.get("messagecourse") == null) {
            return new ResultBody(404, "参数错误", false);
        }
        Object object2 = map.get("messagecourse");
        Integer messagecourse;
        if (object2 instanceof String) {
            messagecourse = Integer.parseInt((String) object2);
        } else if (object2 instanceof Integer) {
            messagecourse = (Integer) object2;
        } else
            return new ResultBody(400, "未知参数类型", false);
        return messageService.displaysmessage(messagecourse);
    }
}